﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumTracker.Models;
using ScrumTracker.Helpers;

namespace ScrumTracker.Controllers
{
    public class TasksController : Controller
    {
        public ActionResult Get(int id)
        {
            var context = new ScrumTrackerEntities();
            ViewData.Model = context.Issues.SingleOrDefault(i => i.IssueId == id) as Task;
            return View();
        }

        public ActionResult New(Task model)
        {
            ModelState.Clear();
            ViewData.Model = model;
            var ctx = ScrumTrackerEntities.Instance;
            ViewData.Add("AssigneeId", ctx.Users.ToList().Select(u => new SelectListItem(){ Value = u.UserId.ToString(), Text = u.FirstName + " " + u.LastName }).ToList());
            return View();
        }

        [HttpPost]
        public ActionResult Create(Task model)
        {
            //var entity = new Sprint() { CreatedAt = DateTime.UtcNow };
            //if (TryUpdateModel<Sprint>(entity))
            //{
            //    var context = ScrumTrackerEntities.Instance;
            //    context.Sprints.AddObject(entity);
            //    context.SaveChanges();
            //    TempData["notice"] = "Sprint successfully added!";
            //    return RedirectToAction("Get", "Sprints", new { id = entity.SprintId });
            //}
            //return Redirect(Request.UrlReferrer.OriginalString + "#sprints");
            //return View("New");
            model.IssueType = (int)IssueType.Task;
            model.CreatedBy = SessionHelper.CurrentUser.UserId;
            model.CreatedAt = DateTime.UtcNow;
            if (ModelState.IsValid)
            {
                var context = ScrumTrackerEntities.Instance;
                context.Issues.AddObject(model);
                context.SaveChanges();
                return RedirectToAction("Get", "Tasks", new { id = model.IssueId });
            }
            var ctx = ScrumTrackerEntities.Instance;
            ViewData.Add("AssigneeId", ctx.Users.ToList().Select(u => new SelectListItem() { Value = u.UserId.ToString(), Text = u.FirstName + " " + u.LastName }).ToList());
            return View("New");
        }

        public ActionResult Edit(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var model = context.Issues.SingleOrDefault(e => e.IssueId == id) as Task;
            ViewData.Model = model;
            ViewData["AssigneeId"] = context.Users.ToList().Select(u => new SelectListItem() { Value = u.UserId.ToString(), Text = string.Format("{0} {1}", u.FirstName, u.LastName), Selected = model.AssigneeId == u.UserId });
            return View();
        }

        [HttpPost]
        public ActionResult Update(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var entity = context.Issues.SingleOrDefault(e => e.IssueId == id) as Task;
            if (TryUpdateModel<Task>(entity))
            {
                context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                context.SaveChanges();
                return RedirectToAction("Get", "Tasks", new { id = entity.IssueId });
            }
            return View("Edit");
        }
    }
}
